package com.corecloud.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corecloud.entity.SysAreaEntity;
import com.corecloud.entity.SysDeptEntity;
import com.corecloud.entity.SysUserEntity;
import com.corecloud.service.SysAreaService;
import com.corecloud.service.SysDeptService;
import com.corecloud.service.SysUserService;
import com.corecloud.tools.AjaxResultData;
import com.corecloud.tools.PageDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;
import java.util.List;


/**
 * 机构管理相关
 */
@Controller
@RequestMapping("/dept")
public class DeptController {


	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysAreaService sysAreaService;
	@Autowired
	private SysUserService sysUserService;


	/**
	 * 机构信息首页
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(Principal principal){
		ModelAndView modelAndView = new ModelAndView("dept/index");
		return modelAndView;
	}




	/**
	 * 机构列表页数据（分页）
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Page<SysDeptEntity> list(Principal principal, SysDeptEntity sd, PageDemo pageDemo){
		QueryWrapper<SysDeptEntity> qw = new QueryWrapper();
		//拼装查询条件
		if(!StringUtils.isEmpty(sd.getIsValid())){
			qw.eq("is_valid",sd.getIsValid());
		}
		if(!StringUtils.isEmpty(sd.getDeptName())){
			qw.like("dept_name",sd.getDeptName());
		}
		if(!StringUtils.isEmpty(sd.getAreaId())){
			qw.eq("area_id",sd.getAreaId());
		}
		OrderItem oi = new OrderItem();
		oi.setAsc(false);
		oi.setColumn("id");
		pageDemo.addOrder(oi);
		return sysDeptService.getInfoByPage(pageDemo,qw);
	}



	/**
	 * 添加机构
	 * @param principal
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(Principal principal,String aid){
		ModelAndView modelAndView = new ModelAndView("dept/add");
		//查询地域信息
		if(StringUtils.isEmpty(aid)){
			aid = "410000";
		}
		modelAndView.addObject("areaInfo",sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id",aid)));
		return modelAndView;
	}


	/**
	 * 保存机构信息
	 * @param principal
	 * @param info
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResultData save(Principal principal, SysDeptEntity info) {
		if(StringUtils.isEmpty(info.getDeptName())){
			return new AjaxResultData(500,"请填写机构名称");
		}
		if(StringUtils.isEmpty(info.getAreaId())){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		SysDeptEntity deptInfo;
		//如果用户填入机构代码，则验证是否已存在
		if(!StringUtils.isEmpty(info.getDeptCode())){
			deptInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("dept_code",info.getDeptCode().trim()));
			if(deptInfo != null && !StringUtils.isEmpty(deptInfo.getDeptCode())){
				return new AjaxResultData(500,"机构代码已被占用");
			}
		}
		//验证机构名称是否被占用
		deptInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("dept_name",info.getDeptName().trim()));
		if(deptInfo != null && !StringUtils.isEmpty(deptInfo.getDeptName())){
			return new AjaxResultData(500,"机构名称已被占用");
		}

		try {
			deptInfo = new SysDeptEntity();
			deptInfo.setAreaId(info.getAreaId().trim());
			deptInfo.setDeptAddress(info.getDeptAddress().trim());
			deptInfo.setDeptCode(info.getDeptCode().trim());
			deptInfo.setDeptName(info.getDeptName().trim());
			deptInfo.setIsValid(info.getIsValid());
			deptInfo.setDeptPhone(info.getDeptPhone().trim());
			deptInfo.setDeptType(info.getDeptType().trim());
			sysDeptService.insertInfo(deptInfo);
			return new AjaxResultData(200,"操作成功");
		} catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}


	/**
	 * 修改机构
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(Principal principal,Integer id){
		ModelAndView modelAndView = new ModelAndView("dept/edit");
		//查询信息,回填页面
		if(!StringUtils.isEmpty(id)){
			SysDeptEntity deptInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("id",id));
			modelAndView.addObject("deptInfo",deptInfo);
			modelAndView.addObject("areaInfo",sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id",deptInfo.getAreaId())));

		}
		return modelAndView;
	}



	/**
	 * 修改机构信息
	 * @param principal
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateInfo")
	@ResponseBody
	public AjaxResultData updateInfo(Principal principal, SysDeptEntity info) {
		if(StringUtils.isEmpty(info.getId())){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		if(StringUtils.isEmpty(info.getDeptName())){
			return new AjaxResultData(500,"请填写机构名称");
		}
		if(StringUtils.isEmpty(info.getAreaId())){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		SysDeptEntity deptInfo;
		SysDeptEntity oldInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("id",info.getId()));
		//如果用户填入机构代码，则验证是否已存在
		if(!StringUtils.isEmpty(info.getDeptCode())){
			deptInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("dept_code",info.getDeptCode().trim()));
			if(deptInfo != null && !deptInfo.getId().equals(oldInfo.getId())){
				return new AjaxResultData(500,"机构代码已被占用");
			}
		}
		//验证机构名称是否被占用
		deptInfo = sysDeptService.selectOne(new QueryWrapper<SysDeptEntity>().eq("dept_name",info.getDeptName().trim()));
		if(deptInfo != null && !deptInfo.getId().equals(oldInfo.getId())){
			return new AjaxResultData(500,"机构名称已被占用");
		}
		try {
			//重置机构信息
			oldInfo.setDeptAddress(info.getDeptAddress().trim());
			oldInfo.setDeptCode(info.getDeptCode().trim());
			oldInfo.setDeptName(info.getDeptName().trim());
			oldInfo.setIsValid(info.getIsValid());
			oldInfo.setDeptPhone(info.getDeptPhone().trim());
			oldInfo.setDeptType(info.getDeptType().trim());
			sysDeptService.updateInfo(oldInfo);
			return new AjaxResultData(200,"操作成功");
		} catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}


	/**
	 * 删除机构信息
	 * @param principal
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResultData delete(Principal principal, Integer id) {
		if (StringUtils.isEmpty(id)) {
			return new AjaxResultData(500, "参数异常,请联系管理员");
		}
		try {
			List<SysUserEntity> userList = sysUserService.selectList(new QueryWrapper<SysUserEntity>().eq("dept_id",id));
			if(userList != null && userList.size() > 0){
				return new AjaxResultData(500, "该机构下有用户挂靠,请确认后删除");
			}
			sysDeptService.deleteInfo(id);
			return new AjaxResultData(200,"操作成功");
		} catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}




}
